package cn.iocoder.yudao.module.ai.controller.admin.label;

import cn.iocoder.yudao.module.ai.controller.admin.label.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.label.LabelDO;
import cn.iocoder.yudao.module.ai.dal.dataobject.label.LabelGroupDO;
import cn.iocoder.yudao.module.ai.dal.dataobject.label.LabelRelationDO;
import cn.iocoder.yudao.module.ai.dal.mysql.label.LabelMapper;
import cn.iocoder.yudao.module.ai.service.label.LabelGroupService;
import cn.iocoder.yudao.module.ai.service.label.LabelRelationService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 标签组管理")
@RestController
@RequestMapping("/ai/label-group")
@Validated
public class LabelGroupController {

    @Resource
    private LabelGroupService labelGroupService;
    @Resource
    private LabelMapper labelMapper;
    @Resource
    private LabelRelationService labelRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建标签组管理")
    @PreAuthorize("@ss.hasPermission('ai:label-group:create')")
    public CommonResult<Long> createLabelGroup(@Valid @RequestBody LabelGroupSaveReqVO createReqVO) {
        return success(labelGroupService.createLabelGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签组管理")
    @PreAuthorize("@ss.hasPermission('ai:label-group:update')")
    public CommonResult<Boolean> updateLabelGroup(@Valid @RequestBody LabelGroupSaveReqVO updateReqVO) {
        labelGroupService.updateLabelGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标签组管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:label-group:delete')")
    public CommonResult<Boolean> deleteLabelGroup(@RequestParam("id") Long id) {
        labelGroupService.deleteLabelGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标签组管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:label-group:query')")
    public CommonResult<LabelGroupRespVO> getLabelGroup(@RequestParam("id") Long id) {
        LabelGroupDO labelGroup = labelGroupService.getLabelGroup(id);
        LabelGroupRespVO bean = BeanUtils.toBean(labelGroup, LabelGroupRespVO.class);
        List<LabelDO> labelDOS = labelMapper.selectAllByGroupId(id);
        if(CollectionUtils.isNotEmpty(labelDOS)){
            List<LabelRespVO> respVOList = BeanUtils.toBean(labelDOS, LabelRespVO.class);
            bean.setLabels(respVOList);
        }
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得标签组管理分页")
    @PreAuthorize("@ss.hasPermission('ai:label-group:query')")
    public CommonResult<PageResult<LabelGroupRespVO>> getLabelGroupPage(@Valid LabelGroupPageReqVO pageReqVO) {
        PageResult<LabelGroupDO> pageResult = labelGroupService.getLabelGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LabelGroupRespVO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出标签组管理 Excel")
//    @PreAuthorize("@ss.hasPermission('ai:label-group:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportLabelGroupExcel(@Valid LabelGroupPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<LabelGroupDO> list = labelGroupService.getLabelGroupPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "标签组管理.xls", "数据", LabelGroupRespVO.class,
//                        BeanUtils.toBean(list, LabelGroupRespVO.class));
//    }

}